﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using IDBHelper;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.ObjectModel;
using WpfApp1.Models;
namespace WpfApp1.ViewModels
{
    public partial class MainViewModel : ObservableObject
    {
        [ObservableProperty]
        public string _Title = "DI";

        private string _Content = "IOC容器";

        public string Content
        {
            get => _Content;
            set { SetProperty(ref _Content, value); }
        }

        public string ImagePath => "Images/1.png";

        private const int PageSize = 5;
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(PageData))]
        private int _PageNumber = 2;

        public int PageCount => Persons.Count % PageSize == 0 ? Persons.Count / PageSize : Persons.Count / PageSize + 1;

        public ObservableCollection<Person> PageData
        {
            get => new(Persons.Skip((PageNumber - 1) * PageSize).Take(PageSize));
        }

        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(PageCount))]
        [NotifyPropertyChangedFor(nameof(PageData))]
        private ObservableCollection<Person> _Persons = [
            new() { Name="Tom",College="数学学院",Gender=1,BirthDate=new DateTime(2007,10,12) },
                new() { Name="Jory",College="数学学院",Gender=2,BirthDate=new DateTime(2009,11,09) },
                new() { Name="Alex",College="数学学院",Gender=2,BirthDate=new DateTime(2008,7,19) },
                new() { Name="Tom",College="数学学院",Gender=1,BirthDate=new DateTime(2007,10,12) },
                new() { Name="Jory",College="数学学院",Gender=2,BirthDate=new DateTime(2009,11,09) },
                new() { Name="Alex",College="数学学院",Gender=2,BirthDate=new DateTime(2008,7,19) },
                new() { Name="Tom",College="数学学院",Gender=1,BirthDate=new DateTime(2007,10,12) },
                new() { Name="Jory",College="数学学院",Gender=2,BirthDate=new DateTime(2009,11,09) },
                new() { Name="Alex",College="数学学院",Gender=2,BirthDate=new DateTime(2008,7,19) },
                new() { Name="Tom",College="数学学院",Gender=1,BirthDate=new DateTime(2007,10,12) },
                new() { Name="Jory",College="数学学院",Gender=2,BirthDate=new DateTime(2009,11,09) },
                new() { Name="Alex",College="数学学院",Gender=2,BirthDate=new DateTime(2008,7,19) },
                new() { Name="Tom",College="数学学院",Gender=1,BirthDate=new DateTime(2007,10,12) },
                new() { Name="Jory",College="数学学院",Gender=2,BirthDate=new DateTime(2009,11,09) },
                new() { Name="Alex",College="数学学院",Gender=2,BirthDate=new DateTime(2008,7,19) },
                new() { Name="Tison",College="数学学院",Gender=1,BirthDate=new DateTime(2008,2,14) }
            ];

        /// <summary>
        /// 数据库操作，Insert、Delete、Update、Query
        /// </summary>
        /// <param name="method"></param>
        [RelayCommand]
        private void DbHandle(string method)
        {
            //自IOC容器中获取IDbHelper接口
            IDbHelper db = App.Current.ServiceProvider.GetRequiredService<IDbHelper>();

            Type type = db.GetType();
            if (type != null)
            {
                var modth = type.GetMethod(method);
                modth?.Invoke(db, null);
            }

            Persons = [
                new() { Name="Tom",College="数学学院",Gender=1,BirthDate=new DateTime(2007,10,12) },
                    new() { Name="Jory",College="数学学院",Gender=2,BirthDate=new DateTime(2009,11,09) },
                    new() { Name="Alex",College="数学学院",Gender=2,BirthDate=new DateTime(2008,7,19) },
                    new() { Name="Tom",College="数学学院",Gender=1,BirthDate=new DateTime(2007,10,12) },
                    new() { Name="Jory",College="数学学院",Gender=2,BirthDate=new DateTime(2009,11,09) },
                    new() { Name="Alex",College="数学学院",Gender=2,BirthDate=new DateTime(2008,7,19) },
                    ];

            if (PageNumber > PageCount) PageNumber = PageCount;
        }

        [RelayCommand]
        private void Delete(Person record)
        {
            Persons.Remove(record);
            OnPropertyChanged(nameof(PageCount));
            OnPropertyChanged(nameof(PageData));
            if (PageNumber > PageCount) PageNumber = PageCount;
        }

    }
}
